package com.admin.web.controller;

import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.goods.Spu;
import com.bayss.core.model.SpuQuery;
import com.bayss.core.service.sku.SpuService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * spu
 */
@RestController
public class SpuController  extends ApiBaseController{
    @Autowired
    private SpuService spuService;

    /**
     * spu删除
     * @param id
     * @return
     */
    @GetMapping(value = "/spu/delete.json")
    public ResultMsg delete(Integer id) {
        try {
            spuService.deleteById(id);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "删除失败");
        }
    }

    /**
     * spu新增
     * @param spu
     * @return
     */
    @PostMapping(value = "/spu/add.json")
    public ResultMsg add(@RequestBody Spu spu) {
        return spuService.add(spu);
    }

    /**
     * spu更新
     * @param spu
     * @return
     */
    @PostMapping(value = "/spu/update.json")
    public ResultMsg update(@RequestBody Spu spu) {
        spuService.update(spu);
        return ResultMsg.success();
    }

    /**
     * spu查询全部
     * @param spuCode
     * @return
     */
    @GetMapping(value = "/spu/list.json")
    public ResultMsg list(String spuCode, String name, Integer id) {
        try {
            Spu spuQuery = new Spu();
            spuQuery.setId(id);
            spuQuery.setSpuNameCn(name);
            spuQuery.setSpuCode(spuCode);
            return ResultMsg.success(spuService.list(spuQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250, "提交失败");
        }
    }

    /**
     * spu分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/spu/getByPage.json")
    public ResultMsg getByPage(Integer page, Integer pageSize,Integer id,String spuName) {
        try {
            SpuQuery spuQuery = SpuQuery.builder().id(id).spuName(spuName).build();
            PageInfo<Spu> pageInfo = spuService.getByPage(page, pageSize,spuQuery);
            return ResultMsg.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1, "查询失败");
        }
    }
}
